import { useUserStore } from "@/stores";
import useStomp from "@/utils/stomp";

const baseURL = import.meta.env.VITE_WS_BASE_URL;
const userStore = useUserStore();

let stomp;

export default function useGlobalConnection(handleChatMessage) {
  if (!stomp) {
    stomp = useStomp(`${baseURL}/chat/api/me/ws?token=Bearer ${userStore.token}`, '/queue/user', handleChatMessage);
    stomp.connect();
  }

  const chatSend = ({conversationId, content}) => {
    stomp.send('/app/global/chat', {conversationId, content})
  }

  return {
    chatSend
  }
}